<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb class="navigation">
      <el-breadcrumb-item :to="{ path: '/mapm' }">管理员首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体区域 -->
    <el-card class="notice">
      <h4>&nbsp;&nbsp;管理员列表</h4>
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.txt" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">新增管理员</el-button>
        </el-col>
      </el-row>
      <!--用户列表区-->
      <el-table :data="userlist" border stripe>
        <el-table-column label="管理员id" prop="id" width="150px"> </el-table-column>
        <el-table-column label="姓名" prop="username"> </el-table-column>
        <el-table-column label="账号" prop="idNumber"> </el-table-column>
        <el-table-column label="创建时间" prop="createTime"> </el-table-column>
        <el-table-column label="角色" prop="roleName"> </el-table-column>
        <el-table-column label="操作" width="270px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)">
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- 添加管理员的对话框 -->
      <el-dialog title="添加管理员" :visible.sync="addDialogVisible" width="30%" @close="addDialogClosed">
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="90px">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="账号" prop="telephone">
            <el-input v-model="addForm.telephone"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <el-form-item label=" 管理员类型" prop="roleName">
          <el-select v-model="addForm.roleName" style="width: 52%" placeholder="请选择管理员类型">
            <el-option v-for="item in list" :key="item.roleName" :value="item.roleName">
              {{ item.roleName }}
            </el-option>
          </el-select>
        </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改管理员的对话框 -->
      <el-dialog title="修改管理员" :visible.sync="editDialogVisible" width="30%" @close="editDialogClosed">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
          <el-form-item label="管理员id" prop="id">
            <el-input v-model="editForm.id" disabled></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="username">
            <el-input v-model="editForm.username"></el-input>
          </el-form-item>
          <el-form-item label="账号" prop="telephone">
            <el-input v-model="editForm.telephone"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="editForm.password"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Administrator',
  data () {
    return {
      // 获取管理员列表的参数
      queryInfo: {
        txt: '',
        // 当前页码
        pageNum: 1,
        // 当前每页显示多少条数据
        pageSize: 10
      },
      userlist: [],
      total: 0,
      list: [],
      // 控制添加管理员对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改管理员对话框的显示与隐藏
      editDialogVisible: false,
      // 添加管理员的表单数据
      addForm: {
        username: '',
        telephone: '',
        password: '',
        roleName: ''
      },
      // 添加表单的管理员规则对象
      addFormRules: {
        username: [
          { max: 12, min: 3, message: '用户名的长度为3到12位', trigger: 'blur' },
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { max: 12, min: 6, message: '密码的长度为6到12位', trigger: 'blur' },
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        number: [
          { max: 12, min: 4, message: '密码的长度为4到12位', trigger: 'blur' },
          { required: true, message: '请输入账号', trigger: 'blur' }
        ]
      },
      // 修改表单的管理员规则对象
      editFormRules: {
        username: [
          { max: 12, min: 3, message: '用户名的长度为3到12位', trigger: 'blur' },
          { required: true, message: '请输入姓名', trigger: 'blur' }
          // { validator: validatePass, trigger: 'blur' }
        ],
        password: [
          { max: 12, min: 6, message: '密码的长度为6到12位', trigger: 'blur' },
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        number: [
          { max: 12, min: 4, message: '密码的长度为4到12位', trigger: 'blur' },
          { required: true, message: '请输入账号', trigger: 'blur' }
        ]
      },
      // 查询到的用户信息对象
      editForm: {},
      // 控制分配角色的对话框显示与否
      setRoleDialogVisible: false,
      // 需要被分配角色的管理员信息
      managerInfo: {},
      // 所有角色的数据列表
      rolesList: [],
      // 已选中的角色ID值
      selectedRoleId: ''
    }
  },
  created () {
    this.getUserList()
  },
  mounted () {
    this.getSelect()
  },
  methods: {
    // 获取用户列表
    async getUserList () {
      const { data: res } = await this.$axios.get('searchbypage/manager', {
        params: this.queryInfo
      })
      if (res.code !== 200) {
        return this.$message.error('获取管理员列表失败！')
      }
      console.log(res)
      this.userlist = res.data.list
      this.total = res.data.total
    },
    // 监听pagesize改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getUserList()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getUserList()
    },
    // 监听添加用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 请求管理员类型数据
    getSelect () {
      this.$axios.get('admin/role')
        .then(res => {
          if (res.data) {
            this.list = res.data.data
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 点击按钮添加新用户
    addUser () {
      this.$refs.addFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$axios.post('admin/manager', this.addForm)
          if (res.code !== 200) {
            this.$message.error(res.message)
          }
          this.$message.success(res.message)
          this.addDialogVisible = false
          this.getUserList()
        }
      })
    },
    // 展示编辑用户的对话框
    async showEditDialog (id) {
      const { data: res } = await this.$axios.get('admin/manager/' + id)
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('查询用户信息失败！')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户信息并提交
    editUserInfo () {
      this.$refs.editFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$axios.put('admin/manager', {
            id: this.editForm.id,
            username: this.editForm.username,
            telephone: this.editForm.number,
            password: this.editForm.password
          })
          if (res.code !== 200) {
            this.$message.error(res.message)
          }
          this.$message.success('添加用户成功！')
          this.editDialogVisible = false
          this.getUserList()
        }
      })
    },
    // 根据id删除对应的用户信息
    async removeUserById (id) {
      // 弹框询问
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?', '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => {
        return err
      })
      // 如果用户确认删除，则返回值为字符串confirm
      // 如果用户确认删除，则返回值为字符串cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$axios.delete('admin/manager/' + id)
      if (res.code !== 200) {
        return this.$message.error(res.message)
      }
      this.$message.success('删除管理员成功！')
      this.getUserList()
    }
  }
}
</script>

<style scoped>
.navigation {
  padding: 10px;
}

.notice {
  margin-bottom: 40px;
  border-radius: 15px;
  box-shadow: 15px 15px 10px -12px #4c61a0;
}

h4 {
  padding-bottom: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid #4c61a0;
}

/* 步骤条 */
.steps {
  margin-left: 20%;
  margin-top: 80px;
  margin-bottom: 60px;
  width: 50%;
}

/* 搜索框 */
.el-table {
  margin-top: 15px;
}

/* 页码 */
.el-pagination {
  margin-top: 15px;
}
</style>
